// CyTargetLinker,
// a Cytoscape plugin to extend biological networks with regulatory interaction
// Copyright 2011-2012 BiGCaT Bioinformatics
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
package org.cytoscape.cytargetlinker.internal.dialog;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Component;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.util.Collections;

import javax.swing.Icon;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JSpinner;
import javax.swing.SpinnerModel;
import javax.swing.SpinnerNumberModel;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;

import org.cytoscape.application.swing.CytoPanelComponent;
import org.cytoscape.application.swing.CytoPanelName;
import org.cytoscape.cytargetlinker.internal.CTLManager;
import org.cytoscape.cytargetlinker.internal.RINHandler;
import org.cytoscape.cytargetlinker.internal.layout.TableLayout;
import org.cytoscape.cytargetlinker.internal.model.RegNet;
import org.cytoscape.cytargetlinker.internal.util.Preferences;
import org.pdf.forms.gui.properties.customcomponents.colorcombobox.ColorCellRenderer;
import org.pdf.forms.gui.properties.customcomponents.colorcombobox.ColorComboBoxEditor;

/**
 * tab in the result panel
 * shows the number of added interactions
 * for each RIN
 * possibility to change the edge colors
 */
public class ResultTab extends JPanel implements CytoPanelComponent {
	private CTLManager linker;
//	private CellConstraints cc;
	private JComboBox colorComboBox;
	private JSpinner field;
	
	//show the databaseColor panel
	public ResultTab() {
	}
	
	// Create a database and color panel
	private JPanel getDatabasePanel() {
		JPanel panel = new JPanel();
		panel.setBackground(Color.WHITE);
		panel.setLayout(new BorderLayout());
		panel.add(getDataColorPanel(), BorderLayout.CENTER);
		
		return panel;
	}

	private Component getDataColorPanel() {
		JPanel panel = new JPanel();
		panel.setLayout(new BorderLayout());
		panel.setBackground(Color.white);
		
		panel.add(fillPanel(), BorderLayout.NORTH);
		
		JScrollPane pane = new JScrollPane(panel);
		pane.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
		pane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED);
		
		return pane;
	}

	private JPanel fillPanel() {
		
		JPanel panel = new JPanel();
		
		double rows[] = new double[6 + linker.getRins().size()];
		rows[0] = 5;
		rows[rows.length-3] = 5;
		rows[rows.length-2] = 75;
		rows[rows.length-1] = 75;
		rows[1] = 75;
		rows[2] = 40;
		
		for(int i = 0; i < linker.getRins().size(); i++) {
			rows[3+i] = 75;
		}
		
		double size[][] = {{5, 75, 30, 30, 50, 5}, // Columns
				rows}; // Rows

		panel.setLayout(new TableLayout(size));
		
//		frame.add(button[0], "1, 1");
//        frame.add(button[1], "1, 5");
//        frame.add(button[2], "1, 3");
//        frame.add(button[3], "5, 3");
//        frame.add(button[4], "3, 3");
		
		
//		String rowLayout = "p,15dlu";
//		for(int i = 0; i < linker.getRins().size(); i++) {
//			rowLayout = rowLayout + ",p,5dlu";
//		}
//		rowLayout = rowLayout + ", 15dlu, p, 5dlu";
//		FormLayout layout = new FormLayout("pref,10dlu, pref, 10dlu, 30dlu, 10dlu, pref", rowLayout);
		
		//create a column with the name of the database found in the file selected
//		JPanel panel = new JPanel(layout);
		panel.setBackground(Color.WHITE);
		panel.add(new JLabel("RIN"), "2,2");
		panel.add(new JLabel("#"), "2,3");
		panel.add(new JLabel("Color"), "2,4");
		panel.add(new JLabel("Visibility"), "2,5");
//		panel.add(new JLabel("RIN"), cc.xy(1, 1));
//		panel.add(new JLabel("#"), cc.xy(3, 1));
//		panel.add(new JLabel("Color"), cc.xy(5, 1));

		int i = 0;
		Collections.sort(linker.getRins());
		
		for(final RegNet net : linker.getRins()) {
//			panel.add(new JLabel(net.getName()), cc.xy(1, ((i+1)*2)+1));
//			panel.add(new JLabel(""+ net.getNewInteractions()), cc.xy(3, ((i+1)*2)+1));
			panel.add(new JLabel(net.getName()), i+4 + ",2");
			panel.add(new JLabel(""+ net.getNewInteractions()), i+4 + ",3");
			colorComboBox = new JComboBox(Preferences.getInstance().getColors().toArray());
			colorComboBox.setEditable(true);
			colorComboBox.setRenderer(new ColorCellRenderer());
		    //show the initial color in the first case of the JComboBox
			colorComboBox.setEditor(new ColorComboBoxEditor(net.getColor()));
			// change the initial color
			colorComboBox.setSelectedItem(net.getColor());

			colorComboBox.addItemListener(new ItemListener() {
				public void itemStateChanged(ItemEvent itemEvent) {
					
						if(itemEvent.getStateChange() == ItemEvent.SELECTED) {
							JComboBox source = (JComboBox)itemEvent.getSource();
							int color = source.getSelectedIndex();

							net.setColor(Preferences.getInstance().getColors().get(color));
							linker.applyVisualStyle();
						}
					}
			});
//			panel.add(colorComboBox, cc.xy(5, ((i + 1) * 2) + 1));
			panel.add(colorComboBox, i+4 + ",4");
			
			String[] show = { "Show", "Hide" };
			JComboBox box = new JComboBox(show);
			box.setSelectedIndex(0);
			box.addItemListener(new ItemListener() {
				
				@Override
				public void itemStateChanged(ItemEvent itemEvent) {
					JComboBox source = (JComboBox)itemEvent.getSource();
					if(source.getSelectedItem().equals("Show")) {
						net.setHidden(false);
						RINHandler.getHandler().showRIN(net, linker);
						RINHandler.getHandler().applyThreshold(linker);
						linker.applyVisualStyle();
					} else {
						net.setHidden(true);
						RINHandler.getHandler().hideRIN(net, linker);
						RINHandler.getHandler().applyThreshold(linker);
						linker.applyVisualStyle();
					}
				}		
				
			});
			
//			panel.add(box, cc.xy(7, ((i+1)*2)+1));
			panel.add(box, i+4 + ",5");
			i++;	
		}

		// (initial value, min, max, step size)
		SpinnerModel model = new SpinnerNumberModel(1, 1, linker.getRins().size()+1, 1);
		field = new JSpinner(model);
		field.addChangeListener(new ChangeListener() {
			
			@Override
			public void stateChanged(ChangeEvent arg0) {
				Integer i = (Integer) field.getValue();
				linker.setThreshold(i);
				RINHandler.getHandler().applyThreshold(linker);
				linker.applyVisualStyle();
				linker.applyLayout();
			}
		});
//		rows.length-1
//		panel.add(new JLabel("Supported by"), cc.xy(1, 4 + (linker.getRins().size()*2)));
//		panel.add(field, cc.xy(3, 4 + (linker.getRins().size()*2)));
//		panel.add(new JLabel("RINs"), cc.xy(5, 4 + (linker.getRins().size()*2)));
		panel.add(new JLabel("Supported by"), rows.length-1 + ",2");
		panel.add(field, rows.length-1 + ",2");
		panel.add(new JLabel("RINs"), rows.length-1 + ",3");
		
		return  panel;
	}

	public void update(CTLManager manager) {
//		cc = new CellConstraints();
		linker = manager;
		this.removeAll();
		this.add(getDatabasePanel());
		this.revalidate();
	}

	@Override
	public Component getComponent() {
		return new JPanel();
	}

	@Override
	public CytoPanelName getCytoPanelName() {
		return CytoPanelName.EAST;
	}

	@Override
	public Icon getIcon() {
		return null;
	}

	@Override
	public String getTitle() {
		return "CyTargetLinker Results";
	}
}
